<template>
  <el-form :model="adInfo" :rules="rules" ref="form" label-width="80px">
    <el-form-item label="广告名称" prop="name">
      <el-input v-model="adInfo.name"></el-input>
    </el-form-item>
    <el-form-item label="广告位置">
      <el-select v-model="adInfo.spaceId" placeholder="请选择">
        <el-option
          v-for="item in spaceList"
          :key="item.id"
          :label="item.name"
          :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="开始时间" prop="startTime">
      <el-date-picker
        v-model="adInfo.startTime"
        type="datetime"
        placeholder="选择日期时间"
        default-time="12:00:00">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="到期时间" prop="endTime">
      <el-date-picker
        v-model="adInfo.endTime"
        type="datetime"
        placeholder="选择日期时间"
        default-time="12:00:00">
      </el-date-picker>
    </el-form-item>
    <el-form-item label="特殊资源">
      <el-radio-group v-model="adInfo.status">
        <el-radio :label="0">下线</el-radio>
        <el-radio :label="1">上线</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="介绍封面">
      <ad-image
        :limit="5"
        v-model="adInfo.img"
      />
    </el-form-item>
    <el-form-item label="广告链接" prop="link">
      <el-input v-model="adInfo.link"></el-input>
    </el-form-item>
    <el-form-item label="广告备注">
      <el-input
        type="textarea"
        v-model="adInfo.text"
        label="请输入备注"
      ></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit">提交</el-button>
    </el-form-item>
  </el-form>
</template>

<script lang='ts'>
import Vue from 'vue'
import { createOrUpdateAd, getEditAdInfo, getAllSpaces } from '@/services/ads'
import AdImage from '../course/components/CourseImage.vue'
import dayjs from 'dayjs'

export default Vue.extend({
  data () {
    return {
      isEdit: !!this.$route.params.id,
      spaceList: [],
      adInfo: {
        id: '',
        name: '',
        spaceId: '',
        img: '',
        startTime: '',
        endTime: '',
        startTimeFormatString: '',
        endTimeFormatString: '',
        status: 0,
        link: '',
        text: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入广告名称', trigger: 'blur' }
        ],
        startTimeFormatString: [
          { type: 'date', required: true, message: '请选择开始时间', trigger: 'change' }
        ],
        endTimeFormatString: [
          { type: 'date', required: true, message: '请选择结束时间', trigger: 'change' }
        ],
        link: [
          { required: true, message: '请输入广告链接', trigger: 'blur' }
        ]
      }
    }
  },
  components: {
    AdImage
  },
  created () {
    this.getAdInfo()
  },
  methods: {
    async getAdInfo () {
      const { data } = await getAllSpaces()
      this.spaceList = data.content
      if (this.isEdit) {
        const id = this.$route.params.id
        const { data } = await getEditAdInfo(id)
        this.adInfo = {
          ...data.content,
          startTime: new Date(data.content.startTime),
          endTime: new Date(data.content.endTime)
        }
      }
    },
    onSubmit () {
      const form: any = this.$refs.form
      if (form) {
        form.validate(async (valid: any) => {
          if (valid) {
            const startTime = dayjs(this.adInfo.startTime)
            const endTime = dayjs(this.adInfo.endTime)
            const { data } = await createOrUpdateAd({
              ...this.adInfo,
              startTime: startTime.format('YYYY-MM-DDThh:mm:ss'),
              startTimeFormatString: startTime.format('YYYY-MM-DD hh:mm:ss'),
              endTime: endTime.format('YYYY-MM-DDThh:mm:ss'),
              endTimeFormatString: endTime.format('YYYY-MM-DD hh:mm:ss')
            })
            console.log('data:', data)
            if (data.success === true) {
              this.$message({ type: 'success', message: '修改成功！' })
              this.$router.push({
                name: 'advert'
              })
            }
          } else {
            console.log('error submit!!')
            this.$message({ type: 'warning', message: '请先填写完整！' })
            return false
          }
        })
      }
    }
  }
})
</script>

<style lang="scss" scope>
.el-form{
  padding: 20px;
  background-color: #ffffff;
}
</style>
